Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for tuple ClickHouse #29715

Merged
merged 15 commits into from
Jan 2, 2024

Conversation

mzitnik
Copy link
Contributor

@mzitnik mzitnik commented Dec 12, 2023

Add support for tuple ClickHouse

Thank you for your contribution! Follow this checklist to help us incorporate your contribution quickly and easily:

  • Mention the appropriate issue in your description (for example: addresses #123), if applicable. This will automatically add a link to the pull request in the issue. If you would like the issue to automatically close on merging the pull request, comment fixes #<ISSUE NUMBER> instead.
  • Update CHANGES.md with noteworthy changes.
  • If this contribution is large, please file an Apache Individual Contributor License Agreement.

See the Contributor Guide for more tips on how to make review process smoother.

To check the build health, please visit https://github.com/apache/beam/blob/master/.test-infra/BUILD_STATUS.md

GitHub Actions Tests Status (on master branch)

Build python source distribution and wheels
Python tests
Java tests
Go tests

See CI.md for more information about GitHub Actions CI or the workflows README to see a list of phrases to trigger workflows.

@mzitnik mzitnik marked this pull request as draft December 12, 2023 08:04
@mzitnik mzitnik marked this pull request as ready for review December 12, 2023 09:10
Copy link
Contributor

Assigning reviewers. If you would like to opt out of this review, comment assign to next reviewer:

R: @robertwb for label java.
R: @chamikaramj for label io.

Available commands:

  • stop reviewer notifications - opt out of the automated review tooling
  • remind me after tests pass - tag the comment author after tests pass
  • waiting on author - shift the attention set back to the author (any comment or push by the author will return the attention set to the reviewers)

The PR bot will only process comments in the main thread (not review comments).

@mshustov
Copy link

Cc @Abacn since you've reviewed the last 2 PRs adding ClickHouse data types

Copy link
Contributor

@Abacn Abacn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, did a quick pass and let some comments. However I am going to travel for the next weeks. Would be great if could find another reviewer or until late December

CHANGES.md Outdated
@@ -68,6 +68,7 @@
* Adding support for LowCardinality DataType in ClickHouse (Java) ([#29533](https://github.com/apache/beam/pull/29533)).
* Added support for handling bad records to KafkaIO (Java) ([#29546](https://github.com/apache/beam/pull/29546))
* Add support for generating text embeddings in MLTransform for Vertex AI and Hugging Face Hub models.([#29564](https://github.com/apache/beam/pull/29564))
* Adding support for Tuples DataType in ClickHouse (Java) ([Tuple Support](https://github.com/apache/beam/pull/29715)).
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can combine this with LowCardinality DataType above, e.g.

Adding support for LowCardinality and Tuples DataType in ClickHouse (Java) ([#29533](https://github.com/apache/beam/pull/29533), [#29715](https://github.com/apache/beam/pull/29715)).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Release cut is today so this may not get into 2.53.0. Could leave it as is and fix when the PR is finalized

@@ -112,7 +113,8 @@
* </table>
*
* Nullable row columns are supported through Nullable type in ClickHouse. Low cardinality hint is
* supported through LowCardinality DataType in ClickHouse.
* supported through LowCardinality DataType in ClickHouse supported through Tuple DataType in
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider put the documentation Tuple to the table above. Broken sentence here currently.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please check this sentence

.map(s -> s.trim().replaceAll(" +", "':;"))
.collect(Collectors.toList());
String content =
String.join(",", l).trim().replaceAll("Tuple\\(", "Tuple('").replaceAll(",", ",'");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The caller has a condition if (type.toLowerCase().trim().startsWith("tuple(")) { below, so here payload always (case insensitive) starts with "tuple(", so here the replace of "Tuple\(" won't work

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The condition is on with lower case, but the actual parsing is on the original string

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I mean below when " if (type.toLowerCase().trim().startsWith("tuple(")) {" the this preprocessing will be executed.

So Tuple( (with backslash) is considered here but not the condition in the caller there. Coild this cause issue?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right. Would you mind adding some comments about the proprocessing rule and example, for example, sth like // Tuple(a String, b Integer) -> ...

@@ -99,6 +103,9 @@ TOKEN :
| < EQ : "=" >
| < BOOL : "BOOL" >
| < LOWCARDINALITY : "LOWCARDINALITY" >
| < TUPLE : "TUPLE" >
| < COLON : ":" >
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is COLON and SEMI_COLON for tuple or another feature?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using that since i have some ambiguity in parsing (looking for other options), but for now decided to leave it that way

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If it's get added to SDK then there is expectation of backward compatibility. So I would prefer to keep consistent with ClickHouse's SQL syntax

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a temporary solution since there is some ambiguity when using javacc having difficulty to detecting between tokens and strings

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed COLON and SEMI_ COLON

Map<String, ColumnType> m2 = new HashMap<>();
m2.put("a1", ColumnType.STRING);
m2.put("b", ColumnType.BOOL);
ColumnType columnType02 = ColumnType.parse("Tuple('a1':;String,'b':;Bool)");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is :; a standard syntax for ClickHouse? https://clickhouse.com/docs/en/sql-reference/data-types/tuple seems not mentioning this pattern.

Copy link
Contributor Author

@mzitnik mzitnik Dec 13, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Abacn Removed :;

Copy link
Contributor

Reminder, please take a look at this pr: @robertwb @chamikaramj

Copy link
Contributor

Assigning new set of reviewers because Pr has gone too long without review. If you would like to opt out of this review, comment assign to next reviewer:

R: @kennknowles for label java.
R: @johnjcasey for label io.

Available commands:

  • stop reviewer notifications - opt out of the automated review tooling
  • remind me after tests pass - tag the comment author after tests pass
  • waiting on author - shift the attention set back to the author (any comment or push by the author will return the attention set to the reviewers)

Copy link
Contributor

@Abacn Abacn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, the syntax now looks good and consistent with clickhouse documentation. Replied to previous comment

CHANGES.md Outdated
* Added support for handling bad records to KafkaIO (Java) ([#29546](https://github.com/apache/beam/pull/29546))
* Add support for generating text embeddings in MLTransform for Vertex AI and Hugging Face Hub models.([#29564](https://github.com/apache/beam/pull/29564))
* NATS IO connector added (Go) ([#29000](https://github.com/apache/beam/issues/29000)).
* Adding support for LowCardinality and Tuples DataType in ClickHouse (Java) ([#29533](https://github.com/apache/beam/pull/29533), [#29715](https://github.com/apache/beam/pull/29715)).
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LowCardinality will go to 2.53.0 but Tuples does not, as 2.53.0 is already cut

@@ -112,7 +113,8 @@
* </table>
*
* Nullable row columns are supported through Nullable type in ClickHouse. Low cardinality hint is
* supported through LowCardinality DataType in ClickHouse.
* supported through LowCardinality DataType in ClickHouse supported through Tuple DataType in
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please check this sentence

.map(s -> s.trim().replaceAll(" +", "':;"))
.collect(Collectors.toList());
String content =
String.join(",", l).trim().replaceAll("Tuple\\(", "Tuple('").replaceAll(",", ",'");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I mean below when " if (type.toLowerCase().trim().startsWith("tuple(")) {" the this preprocessing will be executed.

So Tuple( (with backslash) is considered here but not the condition in the caller there. Coild this cause issue?

@Abacn
Copy link
Contributor

Abacn commented Jan 2, 2024

waiting on author

@mzitnik
Copy link
Contributor Author

mzitnik commented Jan 2, 2024

@Abacn
Regarding your last comment, "Tuple\(" replaceAll method expects regex since we are looking for "Tuple\(" it's invalid to place ( without \ in regex and the double \ is because we can not represent single \ in quotes. Correct me if I am wrong here.

@Abacn
Copy link
Contributor

Abacn commented Jan 2, 2024

@Abacn Regarding your last comment, "Tuple(" replaceAll method expects regex since we are looking for "Tuple(" it's invalid to place ( without \ in regex and the double \ is because we can not represent single \ in quotes. Correct me if I am wrong here.

You're right, sorry for confusion

Copy link
Contributor

@Abacn Abacn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@Abacn Abacn merged commit 1c1cfa8 into apache:master Jan 2, 2024
20 checks passed
case TUPLE:
List<Schema.Field> fields =
columnType.tupleTypes().entrySet().stream()
.map(x -> Schema.Field.of(x.getKey(), Schema.FieldType.DATETIME))
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @mzitnik,
Shouldn't each key inside a tuple be mapped to their respective data type ? Any reason for being DATETIME?

Copy link

@wattache wattache Feb 13, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi,

This quick adaptation seems to work:

case TUPLE:
     List<TableSchema.Column> columns = columnType.tupleTypes().entrySet().stream().map(
                              x -> TableSchema.Column.of(x.getKey(), x.getValue())
                         ).collect(Collectors.toList());
     TableSchema tupleSchema = TableSchema.of(columns.toArray(new TableSchema.Column[0]));
     Schema tupleAsRowSchema = getEquivalentSchema(tupleSchema);
     return Schema.FieldType.row(tupleAsRowSchema);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi,

Also, it seems that Array(Tuple(*)) is not supported when calling ClickHouseIO.getTableSchema.

This seems to fix the issue :
if (type.toLowerCase().trim().startsWith("tuple(")) { String content = tuplePreprocessing(type); columnType = TableSchema.ColumnType.parse(content); } else if (type.toLowerCase().trim().startsWith("array(tuple(")) { String content = tuplePreprocessing(type); columnType = TableSchema.ColumnType.parse(content); }

Copy link
Contributor Author

@mzitnik mzitnik Feb 28, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@erube and @wattache, thanks for the feedback i will fork and also add some tests to cover Array(Tuple(*))

@mzitnik mzitnik deleted the add-support-for-tuple-clickhouse branch August 20, 2024 06:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants